package com.example.demo_mysql_mybatisplus.util.auditLog.dto;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;

/**
 * @author yh 🐟
 * @date 2021/10/12 7:22 下午
 */
@Slf4j
@Component
public class DiffParser {

    public static JSONObject  parseValueDiff(String newValue, String rawValue) {
        JSONObject jsonObject = new CampareJson().campareJsonObject(rawValue, newValue);
        return jsonObject;
    }

    public static String  parseStringDiff(String newValue, String rawValue) {
        String s = new CampareJson().campareJsonString(rawValue, newValue);
        return s;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String str1="{\"notebookId\":\"1\",\"productCode\":\"ASPL0002\",\"moduleId\":\"1501829263829303298\",\"moduleBaseId\":\"1501829418158718977\",\"compoundNum\":\"记录本2022031220220313-01\",\"sortId\":\"1379609573270294530\",\"createUser\":\"李锦泉\",\"notebookName\":\"记录本20220312\",\"moduleName\":\"ljq-自由\",\"jsonId\":\"1646898973496\",\"widgetType\":\"customMaterial\",\"type\":\"customMaterial\",\"source\":0,\"moduleValue\":\"{\\\"afterUse\\\":\\\"0.0000\\\",\\\"amountUsed\\\":\\\"0.0000\\\",\\\"auditStatus\\\":\\\"0\\\",\\\"beforeUse\\\":\\\"0.0000\\\",\\\"bottleNo\\\":\\\"-10105\\\",\\\"cas\\\":\\\"110-82-7,12217-02-6,68411-76-7\\\",\\\"coa\\\":\\\"\\\",\\\"code\\\":\\\"WL0000006\\\",\\\"concentration\\\":\\\"10\\\",\\\"concentrationMultiple\\\":\\\"0.0000\\\",\\\"content\\\":\\\" \\\",\\\"createBy\\\":\\\"1471368726536589313\\\",\\\"createTime\\\":\\\"2022-03-13 20:43:49\\\",\\\"createUser\\\":\\\"李锦泉\\\",\\\"decapTime\\\":\\\"2022-03-11 00:00:00\\\",\\\"experimentType\\\":\\\"0\\\",\\\"expressList\\\":[],\\\"fillerType\\\":\\\"0\\\",\\\"handler\\\":\\\"1471368726536589313\\\",\\\"handlerName\\\":\\\"李锦泉\\\",\\\"id\\\":\\\"1502988802455564290\\\",\\\"isDelete\\\":\\\"0\\\",\\\"jsonId\\\":\\\"1646898973496\\\",\\\"managerId\\\":\\\"1471368726536589313\\\",\\\"managerName\\\":\\\"李锦泉\\\",\\\"materialId\\\":\\\"1502234144707637249\\\",\\\"materialType\\\":\\\"1\\\",\\\"moduleBaseId\\\":\\\"1501829418158718977\\\",\\\"name\\\":\\\"zg物料主数据测试01\\\",\\\"notebookId\\\":\\\"1502986528329101314\\\",\\\"preparationer\\\":\\\"1471368726536589313\\\",\\\"preparationerName\\\":\\\"李锦泉\\\",\\\"quantity\\\":\\\"0.0000\\\",\\\"relationId\\\":\\\"1502234144707637249\\\",\\\"sampleHolder\\\":\\\"1471368726536589313\\\",\\\"sampleHolderName\\\":\\\"李锦泉\\\",\\\"startDate\\\":\\\"2022-03-13 00:00:00\\\",\\\"status\\\":\\\"0\\\",\\\"storageCondition\\\":\\\"1\\\",\\\"type\\\":\\\"3\\\",\\\"usedFromProject\\\":\\\"0.0000\\\",\\\"usedFromWarehouse\\\":\\\"0.0000\\\",\\\"usemoduleType\\\":\\\"0\\\",\\\"userName\\\":\\\"李锦泉\\\",\\\"warehouseEntryTime\\\":\\\"2022-03-11 00:00:00\\\",\\\"warehouseId\\\":\\\"1502233727877705729\\\",\\\"materialSource\\\":\\\"0\\\",\\\"saveType\\\":\\\"edit\\\"}\"}";
        String str2="{\"notebookId\":\"2\",\"productCode\":\"ASPL0002\",\"moduleId\":\"1501829263829303298\",\"moduleBaseId\":\"1501829418158718977\",\"compoundNum\":\"记录本2022031220220313-01\",\"sortId\":\"1379609573270294530\",\"createUser\":\"李锦泉\",\"notebookName\":\"记录本20220312\",\"moduleName\":\"ljq-自由\",\"jsonId\":\"1646898973496\",\"widgetType\":\"customMaterial\",\"type\":\"customMaterial\",\"source\":0,\"moduleValue\":\"{\\\"afterUse\\\":\\\"0.0000\\\",\\\"amountUsed\\\":\\\"0.0000\\\",\\\"auditStatus\\\":\\\"0\\\",\\\"beforeUse\\\":\\\"0.0000\\\",\\\"bottleNo\\\":\\\"-10105\\\",\\\"cas\\\":\\\"110-82-7,12217-02-6,68411-76-7\\\",\\\"coa\\\":\\\"\\\",\\\"code\\\":\\\"WL0000006\\\",\\\"concentration\\\":\\\"10\\\",\\\"concentrationMultiple\\\":\\\"0.0000\\\",\\\"content\\\":\\\" \\\",\\\"createBy\\\":\\\"1471368726536589313\\\",\\\"createTime\\\":\\\"2022-03-13 20:43:49\\\",\\\"createUser\\\":\\\"李锦泉\\\",\\\"decapTime\\\":\\\"2022-03-11 00:00:00\\\",\\\"experimentType\\\":\\\"0\\\",\\\"expressList\\\":[],\\\"fillerType\\\":\\\"0\\\",\\\"handler\\\":\\\"1471368726536589313\\\",\\\"handlerName\\\":\\\"李锦泉\\\",\\\"id\\\":\\\"1502988802455564290\\\",\\\"isDelete\\\":\\\"0\\\",\\\"jsonId\\\":\\\"1646898973496\\\",\\\"managerId\\\":\\\"1471368726536589313\\\",\\\"managerName\\\":\\\"李锦泉\\\",\\\"materialId\\\":\\\"1502234144707637249\\\",\\\"materialType\\\":\\\"1\\\",\\\"moduleBaseId\\\":\\\"1501829418158718977\\\",\\\"name\\\":\\\"zg物料主数据测试01\\\",\\\"notebookId\\\":\\\"1502986528329101314\\\",\\\"preparationer\\\":\\\"1471368726536589313\\\",\\\"preparationerName\\\":\\\"李锦泉\\\",\\\"quantity\\\":\\\"0.0000\\\",\\\"relationId\\\":\\\"1502234144707637249\\\",\\\"sampleHolder\\\":\\\"1471368726536589313\\\",\\\"sampleHolderName\\\":\\\"李锦泉\\\",\\\"startDate\\\":\\\"2022-03-13 00:00:00\\\",\\\"status\\\":\\\"0\\\",\\\"storageCondition\\\":\\\"1\\\",\\\"type\\\":\\\"3\\\",\\\"usedFromProject\\\":\\\"0.0000\\\",\\\"usedFromWarehouse\\\":\\\"0.0000\\\",\\\"usemoduleType\\\":\\\"0\\\",\\\"userName\\\":\\\"李锦泉\\\",\\\"warehouseEntryTime\\\":\\\"2022-03-11 00:00:00\\\",\\\"warehouseId\\\":\\\"1502233727877705729\\\",\\\"materialSource\\\":\\\"0\\\",\\\"saveType\\\":\\\"edit\\\"}\"}";
        JSONObject s = new CampareJson().campareJsonObject(str1, str2);
        String str="{\"list\":[{\"notebookId\":\"1\"},{\"notebookId\":\"2\"}]}";
        JSONObject jsonObject = JSONObject.parseObject(str);
        log.info("这个是用JSONObject的parseObject方法来解析JSON字符串!!!");
        for (Object map : jsonObject.entrySet()){
            Object value = ((Map.Entry) map).getValue();
            JSONArray jarr = JSONArray.parseArray(value.toString());
            for (Iterator iterator = jarr.iterator(); iterator.hasNext();) {
                JSONObject job = (JSONObject) iterator.next();
                log.info(job.toString());
            }
        }

        log.info("s = " + s);
    }

    @Getter
    @Setter
    public static class Diff {
        private String newVal;
        private String oldVal;
        private String key;
        private String diffVal;
        private OperationType type;

        public Diff() {
        }

        public Diff(String newVal, String oldVal, String key, OperationType type) {
            this.newVal = newVal;
            this.oldVal = oldVal;
            this.key = key;
            this.type = type;
        }

        public Diff(String newVal, String oldVal, String key, String diffVal, OperationType type) {
            this.newVal = newVal;
            this.oldVal = oldVal;
            this.key = key;
            this.diffVal = diffVal;
            this.type = type;
        }
    }
}
